import XEUtils from 'xe-utils'
const searchVal = {
    searchEvent (listData, community, field) {
        let list1 = []
        // listData 全部的数据， 查的值 community  查找的字段 'l' 
        const filterName = XEUtils.toValueString(community).trim()
        // const filterName = XEUtils.toValueString(this.community).trim().toLowerCase()
        if (filterName) {
            const filterRE = new RegExp(filterName, 'gi')
            const searchProps = [field]
            const rest = listData.filter(item => searchProps.some(key => XEUtils.toValueString(item[key]).toLowerCase().indexOf(filterName) > -1))
                list1 = rest.map(row => {
                    const item = Object.assign({}, row)
                    searchProps.forEach(key => {
                    item[key] = XEUtils.toValueString(item[key]).replace(filterRE, match => `${match}`)
                    })
                    return item
            })
        } else {
        list1 = listData
        }
        return list1
    },
    searchEvents (TableData, searchValue, PropsList) {
        let list1 = []
        const filterName = XEUtils.toValueString(searchValue).trim()
        if (filterName) {
            const filterRE = new RegExp(filterName, 'gi')
            const rest = TableData.filter(item => PropsList.some(key => XEUtils.toValueString(item[key]).toLowerCase().indexOf(filterName) > -1))
                list1 = rest.map(row => {
                    const item = Object.assign({}, row)
                    PropsList.forEach(key => {
                        item[key] = XEUtils.toValueString(item[key]).replace(filterRE, match => `${match}`)
                    })
                    return item
            })
        } else {
            list1 = TableData
        }
        return list1
    },
    searchEvent2 (data, region, field1, community, field2) {
        let list1 = []
        if(region == ''){
            list1 = data
        }else{
            const filterName = XEUtils.toValueString(region).trim()
            const filterRE = new RegExp(filterName, 'gi')
            const searchProps = [field1]
            const rest = data.filter(item => searchProps.some(key => XEUtils.toValueString(item[key]).toLowerCase().indexOf(filterName) > -1))
                list1 = rest.map(row => {
                    const item = Object.assign({}, row)
                    searchProps.forEach(key => {
                    item[key] = XEUtils.toValueString(item[key]).replace(filterRE, match => `${match}`)
                    })
                    return item
            })
        }
        if(community != ''){
            const filterName = XEUtils.toValueString(community).trim()
            const filterRE = new RegExp(filterName, 'gi')
            const searchProps = [field2]
            const rest = data.filter(item => searchProps.some(key => XEUtils.toValueString(item[key]).toLowerCase().indexOf(filterName) > -1))
                list1 = rest.map(row => {
                    const item = Object.assign({}, row)
                    searchProps.forEach(key => {
                    item[key] = XEUtils.toValueString(item[key]).replace(filterRE, match => `${match}`)
                    })
                    return item
            })
        }
        return list1
    }
}
export default searchVal